'''
@Author: ysz
@Date: 2020-01-02 11:59:32
@LastEditors  : Lin Qi Ping
@LastEditTime : 2020-01-03 15:35:09
@Description: 随机生成手机号
'''

#由于数据量太大，存储在列表里，循环数据造成出错
import random
import openpyxl
import re
from openpyxl import load_workbook
class Random_Phone(object):
    
    def __init__(self,filename):
        '''
        @description: 初始化方法，传入要读取的文件路径
        @param1: {
        *      filename:"文件路径",
        *      }
        @return: 无返回值
        @detail: 给对象添加属性
        '''        
        self.file=filename
        self.wk=load_workbook(self.file)

    def obtain_data(self,sheetname):
        '''
        @description: 从指定读取的excel文件中，获取数据
        @param1: {
        *      sheetname:"工作表的名称",
        *      }
        @return: excel数据
        @detail: 读取excel文件，并获取数据
        '''
        phone_list=[]#用于存储所有数据
        sheet=self.wk[sheetname]
        max_row=sheet.max_row
        max_column=sheet.max_column
        for r in range(2,max_row+1):
            phonedict={}#用于存储每行的数据
            for c in range(1,max_column+1):
                key=sheet.cell(1,c).value#取第一行表头数据
                if key:
                    if sheet.cell(r,c).value:
                        key.strip()
                        phonedict[key]=sheet.cell(r,c).value
            phone_list.append(phonedict)
        return phone_list


    def process_data(self,data,city,number):
        ban_phone_list=[]
        phone_list=[]
        print(city)
        print(data[0])
        pattern=re.compile(city)
        for i in data[:9000]:
            if i['地区']==city:
                ban_phone_list.append(i['手机号码段'])
        print(ban_phone_list)
        last_part=[str(i)+str(j)+str(m)+str(n) for i in range(10) for j in range(10) for m in range(10) for n in range(10)]
        # random.shuffle(ban_phone_list)
        # for i in ban_phone_list[:number]:
        #     phone=i[0]+random.choice(last_part)
        #     i[0]=phone
        #     phone_list.append(i)
        # if number<=len(data):
        #     phone_list=phone_list[:number]
        # random.shuffle(phone_list)
        # return phone_list


    def save_data(self,data):
        book=openpyxl.Workbook()
        n=1
        sheet1=book.create_sheet(str(n),n-1)
        sheet1.append(['号段', '省', '城市','卡类型'])
        for item in data:
            print(n,'n的值')
            print(sheet1.max_row)
            if sheet1.max_row==30000:
                sheet1=book.create_sheet(str(n+1),n)
                sheet1.append(['号段', '省', '城市','卡类型'])
                n+=1
            sheet1.append(item)

        book.save('./'+data[0][1]+data[0][2]+'.xlsx')
        

    def run(self,number,city):
        sheetname='Sheet2'
        data=self.obtain_data(sheetname)
        print(len(data))
        self.process_data(data,city,number)
        # print(phone_list)
        # self.save_data(phone_list)
       

if __name__ == "__main__":
    filename='手机号段归属地表.xlsx'
    rp=Random_Phone(filename)
    
    while True:
        number=input("请输入要随机生成的手机号的个数或输入q离开：")
        if number=='q':
            print('欢迎下次再来！！！！')
            break
        city=input("请输入要生成的手机号的归属地城市名称：")
        if number.isdigit():
            rp.run(int(number),city)
            print('存储成功')
        else:
            print('数量或归属地输入有误，请重新输入！！！')
            

        
    